/* ==================================================================== *
 *
 * This file is part of Mapics Framework.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @author Andrey Cherkashin <action.open@gmail.com>
 *
 * ==================================================================== */

/**
 * Abstract class to construct map layer overlay
 */
mapics.MapLayerOverlay = function()
{
    
};

/**
 * Constructs div where to save map objects for this overlay
 *
 * @return HTMLObject
 */
mapics.MapLayerOverlay.prototype.createDiv = function( level )
{
    var div = document.createElement("div");
    div.setAttribute("class", "ui-overlay");
    div.setAttribute("style", "z-index:" + (level+1) + "; position: absolute; display: none;");

    return div;
};

/** ------------------------------------------------------------------------- **
 *  ACTIONS
 ** ------------------------------------------------------------------------- **/

/**
 * Adds new map object on this map overlay
 *
 * @param mapObject (mapics.MapObject)
 *
 * @return mapics.MapLayerOverlay
 */
mapics.MapLayerOverlay.prototype.addObject = function( mapObject )
{
    // Adds object in list, append in div, set overlay
    this.m_MapObjects.push( mapObject );
    this.m_Div.appendChild( mapObject.getDiv() );
    mapObject.setOverlay( this );

    // Update overlay if we already place it on map
    if( this.m_Map != null ) {
        this.update();
    }

    return this;
};

/**
 * Removes map object from this map overlay
 *
 * @param mapObject (mapics.MapObject)
 *
 * @return mapics.MapLayerOverlay
 */
mapics.MapLayerOverlay.prototype.removeObject = function( mapObject )
{
    for(var i = 0, max = this.m_MapObjects.length; i < max; i++)
    {
        if( mapObject == this.m_MapObjects[i] ) {
            this.m_MapObjects.splice(i--, 1);
            break;
        }
    }

    this.m_Div.removeChild( mapObject.getDiv() );
    mapObject.setOverlay( null );

    return this;
};

/** ------------------------------------------------------------------------- **
 *  PROPERTIES
 ** ------------------------------------------------------------------------- **/

/**
 * Sets array of map objects in that map overlay
 *
 * @param mapObjects (Array<mapics.MapObject>)
 *
 * @return mapics.MapLayerOverlay
 */
mapics.MapLayerOverlay.prototype.setMapObjects = function(mapObjects)
{
    var i, max;

    for(i = 0, max = this.m_MapObjects.length; i < max; i++) {
        this.removeObject( this.m_MapObjects[ i ] );
    }

    this.m_MapObjects = mapObjects;

    for(i = 0, max = this.m_MapObjects.length; i < max; i++) {
        this.addObject( this.m_MapObjects[ i ] );
    }

    // Update overlay if we already place it on map
    if( this.m_Map != null ) {
        this.update();
    }

    return this;
};

/**
 * Returns array of map objects in that map overlay
 *
 * @return Array<mapics.MapObject>
 */
mapics.MapLayerOverlay.prototype.getMapObjects = function() {
    return this.m_MapObjects;
};